﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;
using NPOI.HSSF.UserModel;
using OfficeOpenXml;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Threading.Tasks;
namespace GPS.Controllers.GPS_DanhMuc
{
    public class GPS_CoSoDaoTaoController : Controller
    {

        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public GPS.MODELS.ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();
            string userId = User.Identity.GetUserId();
            ApplicationUser _currentUser = dbUser.Users.Find(userId);
            return _currentUser;
        }
        //
        // GET: /GPS_HTCapGPLX/
        #region Index
        public ActionResult Index(ViewModelSDONVIGTVT SearchModel, int? currentPage)
        {
            initialCategorySearchAction(SearchModel);
            var entities = _db.Filter<SDONVIGTVT>(o => (SearchModel.dDVGTVT_MA == null || o.DVGTVT_MA.StartsWith(SearchModel.dDVGTVT_MA))
                                        && (SearchModel.dDVGTVT_TEN == null || o.DVGTVT_TEN.StartsWith(SearchModel.dDVGTVT_TEN))
                                        && (SearchModel.dDVGTVT_MAQL == null || o.DVGTVT_MAQL.StartsWith(SearchModel.dDVGTVT_MAQL))
                                        && (SearchModel.dDVGTVT_ACTIVE == null || o.DVGTVT_ACTIVE == SearchModel.dDVGTVT_ACTIVE)
                                        && o.DVGTVT_LOAI == "DT" && o.DVGTVT_MAQL == "40"
                                        );
            ViewBag.TotalRow = entities.Count();
            var model = new List<SDONVIGTVT>();
            Int16 icount = 1;
            foreach (var currentEntity in entities)
            {
                currentEntity.STT = icount;
                model.Add(Mapper.Map<SDONVIGTVT>(currentEntity));
                icount++;
            }
            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
            return View(SearchModel);
        }

        [HttpPost]
        public void initialCategorySearchAction(ViewModelSDONVIGTVT viewModel)
        {
            ViewBag.lstLoaiDV = new SelectList(_db.Filter<SLOAICSDT>(x => x.LOAICSDT_LOAI == "GTVT" && x.LOAICSDT_MA != "DT" ), "LOAICSDT_MA", "LOAICSDT_TEN");
            //viewModel.lstDVGTVT = _db.Filter<SDONVIGTVT>(x => x.DVGTVT_LOAI == "SO" && x.DVGTVT_MAQL == "40");
            ViewBag.checkuser = this.getCurrentUser().MA_TINHTP;
        }
        #endregion

        //
        // Create: 
        #region create
        public ActionResult Create()
        {
            initialCategoryCreateAction();
            return View(new CrudModelCOSODT());
        }

        [HttpPost]
        public ActionResult Create(CrudModelCOSODT model)
        {
            initialCategoryCreateAction();
            //gán cơ sở đào tạo
            model.DVGTVT_LOAI = "DT";
            if (ModelState.IsValid)
            {
                SDONVIGTVT _model = new SDONVIGTVT();
                GPS.COMMON.Helpers.CopyObject<SDONVIGTVT>(model, ref _model);
                _model.DVGTVT_MA = GetValue_Indentity(model);
                _db.Insert<SDONVIGTVT>(_model);
                return RedirectToAction("Index");
            }
            return View(model);
        }
        public void initialCategoryCreateAction()
        {
            ViewBag.lstLoaiDV = new SelectList(_db.Filter<SLOAICSDT>(x => x.LOAICSDT_LOAI == "GTVT" && x.LOAICSDT_MA != "DT"), "LOAICSDT_MA", "LOAICSDT_TEN");
            ViewBag.lstDVGTVT = new SelectList(_db.Filter<SDONVIGTVT>(x => x.DVGTVT_LOAI == "SO" && x.DVGTVT_MAQL == "40"), "DVGTVT_MAQL", "DVGTVT_TEN");
        }

        public string GetValue_Indentity(CrudModelCOSODT model)
        {
            Int64 valueMax = 0;
            string value = "";
            List<SDONVIGTVT> lst = _db.DbContext.SDONVIGTVTs.Where(x => x.DVGTVT_MAQL == model.DVGTVT_MAQL && x.DVGTVT_LOAI == "DT").ToList();
            if (lst.Count != 0)
            {
                valueMax = lst.Max(e => Convert.ToInt64(e.DVGTVT_MA));
            }
            value = (valueMax + 1).ToString();
            return value;
        }
        #endregion

        //
        // Edit: 
        #region Edit
        public ActionResult Edit(string id)
        {
            CrudModelCOSODT _model = new CrudModelCOSODT();
            SDONVIGTVT model = _db.GetOne<SDONVIGTVT>(o => o.DVGTVT_MA == id);
            GPS.COMMON.Helpers.CopyObject<CrudModelCOSODT>(model, ref _model);
            initialCategoryEditAction(_model);
            return View(_model);
        }
        [HttpPost]
        public ActionResult Edit(CrudModelCOSODT model)
        {
            if (ModelState.IsValid)
            {
                var _model = _db.GetOne<SDONVIGTVT>(o => o.DVGTVT_MA == model.DVGTVT_MA);
                GPS.COMMON.Helpers.CopyObject<SDONVIGTVT>(model, ref _model);
                _db.Update<SDONVIGTVT>(_model);
                return RedirectToAction("Index");
            }
            return View(model);
        }
        public void initialCategoryEditAction(CrudModelCOSODT view)
        {
            string selectedvalue = _db.Filter<SLOAICSDT>(x => x.LOAICSDT_MA == view.DVGTVT_LOAI).FirstOrDefault().ToString();
            ViewBag.lstLoaiDV = new SelectList(_db.Filter<SLOAICSDT>(x => x.LOAICSDT_LOAI == "GTVT"), "LOAICSDT_MA", "LOAICSDT_TEN", selectedvalue);

            string selectedvalueDVGTVT = _db.Filter<SDONVIGTVT>(x => x.DVGTVT_MAQL == view.DVGTVT_MAQL).FirstOrDefault().ToString();
            ViewBag.lstDVGTVT = new SelectList(_db.Filter<SDONVIGTVT>(x => x.DVGTVT_LOAI == "SO" && x.DVGTVT_MAQL == "40"), "DVGTVT_MAQL", "DVGTVT_TEN", selectedvalueDVGTVT);
        }
        #endregion

        #region Detele
        public ActionResult Delete(string id)
        {
            CrudModelCOSODT _model = new CrudModelCOSODT();
            SDONVIGTVT model = _db.GetOne<SDONVIGTVT>(o => o.DVGTVT_MA == id);
            _db.DeleteItem<SDONVIGTVT>(model);
            return RedirectToAction("Index");
        }
        #endregion

        #region Detail
        public ActionResult Detail(string id)
        {
            CrudModelCOSODT _model = new CrudModelCOSODT();
            SDONVIGTVT model = _db.GetOne<SDONVIGTVT>(o => o.DVGTVT_MA == id);
            GPS.COMMON.Helpers.CopyObject<CrudModelCOSODT>(model, ref _model);
            initialCategoryDetailAction(_model);
            return PartialView("Detail", _model);
        }

        public void initialCategoryDetailAction(CrudModelCOSODT view)
        {
            if (view.DVGTVT_ACTIVE == "1")
            {
                view.DVGTVT_ACTIVE = "Hiệu lực";
            }
            else
            {
                view.DVGTVT_ACTIVE = "Không hiệu lực";
            }

            if (view.DVGTVT_LOAICQSH == 1)
            {
                ViewBag.loaiCQSH = "Sát hạch lái xe tất cả các hạng";
            }
            else if (view.DVGTVT_LOAICQSH == 2)
            {
                ViewBag.loaiCQSH = "Sát hạch lái xe đến hạng C";
            }
            else
            {
                ViewBag.loaiCQSH = "Sát hạch lái xe đến hạng A4";
            }

            string selectedvalue = _db.Filter<SLOAICSDT>(x => x.LOAICSDT_MA == view.DVGTVT_LOAI).Select(x => x.LOAICSDT_TEN).FirstOrDefault().ToString();
            view.DVGTVT_LOAI = selectedvalue;

            string selectedvalueDVGTVT = _db.Filter<SDONVIGTVT>(x => x.DVGTVT_MAQL == view.DVGTVT_MAQL && x.DVGTVT_MAQL == "40").Select(x => x.DVGTVT_TEN).FirstOrDefault().ToString();
            view.DVGTVT_MAQL = selectedvalueDVGTVT;
        }
        #endregion
    }
}